package com.lagou.client;

import com.lagou.model.User;
import com.lagou.model.UserMessage;

import java.io.IOException;
import java.util.Scanner;

/**
 * 客户端的主界面绘制和相应功能的实现
 */
public class ClientView {

    private ClientInitClose cic;

    public ClientView(ClientInitClose cic) {
        this.cic = cic;
    }

    /**
     * 客户端主界面的绘制
     */
    public void clientMainPage() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println(" \n\n\t\t  在线考试系统");
            System.out.println("---------------------------");
            System.out.print("   [1] 学院系统");
            System.out.println("     [2] 管理员系统");
            System.out.println("   [0] 退出系统");
            System.out.println("---------------------------");
            System.out.println("请选择要进行的业务编号：");
            Scanner scanner = ClientScanner.getScanner();
            int choose = scanner.nextInt();
            switch (choose) {
                case 1:
                    System.out.println("正在进入学员系统...");
                    break;
                case 2:
                    System.out.println("正在进入管理员系统...");
                    clientManagerLogin();
                    break;
                case 0:
                    System.out.println("正在退出系统...");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 客户端管理员登录的功能
     */
    private void clientManagerLogin() throws IOException, ClassNotFoundException {
        System.out.println("请输入管理员的账户信息：");
        String username = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码信息：");
        String password = ClientScanner.getScanner().next();
        UserMessage um = new UserMessage("managerCheck", new User(username, password));
        cic.getOos().writeObject(um);
        System.out.println("客户端发送管理员账户信息成功！");
        um = (UserMessage) cic.getOis().readObject();
        if ("success".equals(um.getType())) {
            System.out.println("登录成功，欢迎使用！");
        } else {
            System.out.println("用户名或密码错误！");
        }
    }
}
